package UIPhoto
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_matrix;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class uip_photoEditor extends uif_ecomponent {
		
		protected var _matrix	: uif_matrix;
		private var _photo		: Bitmap;
		private var _rect		: uip_rect;
		
		public function uip_photoEditor(parent:uif_component, 
										images: Object, 
										ax:int=0, ay:int=0, 
										awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			
//			addEvent(ga_events.PHOTOLOADCOMPLETE, doPhotoLoad, app);
			addEvent(uip_events.SCALE_RECT, doScaleRect);
			_matrix = new uif_matrix();
//			if (ga.photos[parentItem.index]) updateFromPhoto(ga.photos[parentItem.index]);
			_rect = createRect(images);

			var _mask: uif_ecomponent = new uif_ecomponent(this, 0, 0, width, height, 'white');
			_rect.mask = _mask;

			addEvent(uip_events.FLIPH, doFlipH);
			addEvent(uip_events.FLIPV, doFlipV);			
		}
		
		public function get rect(): uip_rect {
			return _rect;
		}
		
		public function get rectSize(): Point {
			return _rect.size;
		}
		
		public function get ratio(): Number {
			return _rect.width / _rect.height;
		}
		
		protected function createRect(images: Object): uip_rect {
			return new uip_rect(this, images);
		}
		
		public function get photo(): Bitmap {
			return _photo;
		}
		
		public function resetPhoto(a_photo: Bitmap=null): void {
			_matrix = new uif_matrix();

			if ((a_photo != photo) && _photo && a_photo) _photo.bitmapData.dispose();
			if (a_photo) doSetNewPhoto(a_photo);
			updateFromPhoto();
		}
		
		protected function doSetNewPhoto(a_photo: Bitmap): void {
			_photo = a_photo;
		}
		
		private function flipPhoto(a_scaleX: int, a_scaleY: int): void {
			var tmp: Bitmap = new Bitmap(new BitmapData(photo.bitmapData.width, photo.bitmapData.height));
			var mat: Matrix = new Matrix();
			mat.scale(a_scaleX, a_scaleY);
			mat.translate((a_scaleX==-1)?photo.bitmapData.width:0, 
						(a_scaleY==-1)?photo.bitmapData.height:0);
			tmp.bitmapData.draw(photo, mat);
			photo.bitmapData.draw(tmp);
			tmp.bitmapData.dispose();
			dispatchEvent(new ObjectEvent(uip_events.UPDATEMATRIX, rect.getPosMatrix()));
		}
		
		protected function doFlipH(e: Event): void {
			flipPhoto(-1, 1);
			e.stopPropagation();	
		}
		
		protected function doFlipV(e: Event): void {
			flipPhoto(1, -1);
			e.stopPropagation();	
		}
		
		public function get bmSize(): Point {
			return new Point(photo.bitmapData.width, photo.bitmapData.height);
		}
		
		protected function doScaleRect(e: ObjectEvent): void {
			var curScale: Number = _matrix.getScale();
			var scale: Number = curScale - (e.object as Number) / 10;
			if (scale < 1) scale = 1;
			var bmr: Rectangle = EnterHere(bmSize);
			var bmscale: Number = bmr.width / bmSize.x;
			var center: Point = _rect.transform.matrix.transformPoint(new Point(_rect.width / 2, _rect.height / 2));
			center = new Point((center.x - bmr.x) / bmscale, 
								(center.y - bmr.y) / bmscale);
			
			_matrix = new uif_matrix();
			_matrix.translate(-center.x, -center.y);
			_matrix.scale(scale, scale);
			_matrix.translate(center.x, center.y);
			updateFromPhoto();
		}
		
		public function get matrix(): uif_matrix {
			return _matrix;
		}
		
		public function set matrix(a_matrix: uif_matrix): void {
			_matrix = a_matrix;
			updateFromPhoto();
		}
		
		public function EnterHerePhoto(): Rectangle {
			var inner	: Rectangle = getInner();
			var scale	: Point 	= new Point(inner.width / bmSize.x, inner.height / bmSize.y);
			var result  : Rectangle = new Rectangle();
			var r: Number = scale.x<scale.y?scale.x:scale.y;
			result.width = bmSize.x * r;
			result.height = bmSize.y * r;
			result.x = inner.x + (inner.width - result.width) / 2;
			result.y = inner.y + (inner.height - result.height) / 2;
			return result;
		}
		
		protected function updateFromPhoto(): void {
			if (_photo) {
				var rect	: Rectangle = EnterHerePhoto();
				var mat		: Matrix 	= _matrix.clone();
				mat.scale(rect.width / photo.bitmapData.width, rect.height / photo.bitmapData.height);
				mat.translate(rect.x, rect.y);
				repaint();
				graphics.beginBitmapFill(photo.bitmapData, mat, false, true);
				graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
				graphics.endFill();
			}
		}		
	}
}